/**
 * output package name
 */
package com.kingdee.eas.custom.hr.attendance.client;

import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.ctrl.kdf.table.IRow;
import com.kingdee.bos.ctrl.kdf.table.KDTDefaultCellEditor;
import com.kingdee.bos.ctrl.kdf.table.event.KDTEditEvent;
import com.kingdee.bos.ctrl.kdf.table.event.KDTSelectEvent;
import com.kingdee.bos.ctrl.kdf.table.util.KDTableUtil;
import com.kingdee.bos.ctrl.swing.KDComboBox;
import com.kingdee.bos.ctrl.swing.KDDatePicker;
import com.kingdee.bos.ctrl.swing.KDTimePicker;
import com.kingdee.bos.dao.query.ISQLExecutor;
import com.kingdee.bos.dao.query.SQLExecutorFactory;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.ui.face.CoreUIObject;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.cm.util.KDTableUtils;
import com.kingdee.eas.basedata.master.util.StringUtil;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.client.SysContext;
import com.kingdee.eas.custom.common.Constants;
import com.kingdee.eas.custom.common.KDTableUtilEx;
import com.kingdee.eas.custom.hr.attendance.AttendanceDataLogCollection;
import com.kingdee.eas.custom.hr.attendance.AttendanceDataLogFactory;
import com.kingdee.eas.custom.hr.attendance.AttendanceDataLogInfo;
import com.kingdee.eas.framework.ICoreBase;
import com.kingdee.eas.hr.emp.client.EmployeeClientUtils;
import com.kingdee.eas.hr.time.TimeAttendanceCollection;
import com.kingdee.eas.hr.time.TimeAttendanceFactory;
import com.kingdee.eas.hr.time.TimeAttendanceInfo;
import com.kingdee.eas.hr.time.client.TimeAttendanceUtils;
import com.kingdee.eas.util.SysUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.util.enums.EnumUtils;

/**
 * output class name
 */
public class AttendanceDataLogUI extends AbstractAttendanceDataLogUI {
	private static final Logger logger = CoreUIObject
			.getLogger(AttendanceDataLogUI.class);

	private AdminOrgUnitInfo orgUnit = null;

	private boolean isInclude;

	private AttendanceDataLogCollection infoCollection = new AttendanceDataLogCollection();

	/**
	 * output class constructor
	 */
	public AttendanceDataLogUI() throws Exception {
		super();
		jbInit();
	}

	private void jbInit() throws Exception {
		this.tblAttendance.checkParsed();

		KDComboBox tblRecord_colAttendance_Combox = new KDComboBox();
		tblRecord_colAttendance_Combox
				.setName("tblRecord_colAttendance_Combox");
		// tblRecord_holidayItem_Combox.set
		tblRecord_colAttendance_Combox.setVisible(true);
		tblRecord_colAttendance_Combox.addItems(this.getTimeAttendanceList());
		KDTDefaultCellEditor tblRecord_holidayItem_CellEditor = new KDTDefaultCellEditor(
				tblRecord_colAttendance_Combox);
		this.tblAttendance.getColumn("colAttendance").setEditor(
				tblRecord_holidayItem_CellEditor);

		KDComboBox tblRecord_unitType_Combox = new KDComboBox();
		tblRecord_unitType_Combox.setName("tblRecord_unitType_Combox");

		tblRecord_unitType_Combox.setVisible(true);
		tblRecord_unitType_Combox.addItems(EnumUtils.getEnumList(
				"com.kingdee.eas.hr.time.UnitTypeEnum").toArray());
		KDTDefaultCellEditor tblRecord_unitType_CellEditor = new KDTDefaultCellEditor(
				tblRecord_unitType_Combox);
		this.tblAttendance.getColumn("colTotalUnit").setEditor(
				tblRecord_unitType_CellEditor);

		KDDatePicker tblRecord_sumDate_DatePicker = new KDDatePicker();
		tblRecord_sumDate_DatePicker.setName("tblRecord_sumDate_DatePicker");
		tblRecord_sumDate_DatePicker.setVisible(true);
		tblRecord_sumDate_DatePicker.setEditable(true);
		tblRecord_sumDate_DatePicker.setTimeEnabled(false);
		KDTDefaultCellEditor tblRecord_sumDate_CellEditor = new KDTDefaultCellEditor(
				tblRecord_sumDate_DatePicker);
		this.tblAttendance.getColumn("colSumDate").setEditor(
				tblRecord_sumDate_CellEditor);

		KDTableUtilEx.setNotInputColumn(this.tblAttendance, new String[] {
				"colTotalUnit", "colRecordPerson" });
	}

	/**
	 * output storeFields method
	 */
	public void storeFields() {
		super.storeFields();
	}

	/**
	 * output tblMain_tableClicked method
	 */
	protected void tblMain_tableClicked(
			com.kingdee.bos.ctrl.kdf.table.event.KDTMouseEvent e)
			throws Exception {
		super.tblMain_tableClicked(e);
	}

	/**
	 * output tblMain_activeCellChanged method
	 */
	protected void tblMain_activeCellChanged(
			com.kingdee.bos.ctrl.kdf.table.event.KDTActiveCellEvent e)
			throws Exception {
		super.tblMain_activeCellChanged(e);
	}

	/**
	 * output menuItemImportData_actionPerformed method
	 */
	protected void menuItemImportData_actionPerformed(
			java.awt.event.ActionEvent e) throws Exception {
		super.menuItemImportData_actionPerformed(e);
	}

	/**
	 * output chkInclude_actionPerformed method
	 */
	protected void chkInclude_actionPerformed(java.awt.event.ActionEvent e)
			throws Exception {
		this.isInclude = this.chkInclude.isSelected();
		FilterInfo orgFi = getOutOrgRangePersonFilter(this.orgUnit,
				this.isInclude, true);
		this.mainQuery.getFilter().mergeFilter(orgFi, "or");
		this.tblMain.removeRows();
		this.mainQuery.getFilter().getFilterItems().clear();
	}

	/**
	 * output datePickerStart_dataChanged method
	 */
	protected void datePickerStart_dataChanged(
			com.kingdee.bos.ctrl.swing.event.DataChangeEvent e)
			throws Exception {
		infoCollection.clear();
		if (KDTableUtil.getSelectedRow(this.tblMain) != null) {
			infoCollection = this.getAttendanceDataLogCollection();
			fillTblAttendanceRecord(infoCollection);
		}
	}

	/**
	 * output datePickerEnd_dataChanged method
	 */
	protected void datePickerEnd_dataChanged(
			com.kingdee.bos.ctrl.swing.event.DataChangeEvent e)
			throws Exception {
		infoCollection.clear();
		if (KDTableUtil.getSelectedRow(this.tblMain) != null) {
			infoCollection = this.getAttendanceDataLogCollection();
			fillTblAttendanceRecord(infoCollection);
		}
	}

	/**
	 * output promptOrgUnit_dataChanged method
	 */
	protected void promptOrgUnit_dataChanged(
			com.kingdee.bos.ctrl.swing.event.DataChangeEvent e)
			throws Exception {
		this.orgUnit = ((AdminOrgUnitInfo) this.promptOrgUnit.getValue());
		FilterInfo filter = new FilterInfo();
		filter = getOutOrgRangePersonFilter(this.orgUnit, this.isInclude, true);

		this.mainQuery.getFilter().mergeFilter(filter, "and");
		this.tblMain.removeRows();
		this.mainQuery.getFilter().getFilterItems().clear();
		// KDTableUtil.setSelectedRow(this.tblMain, -1);
	}

	/**
	 * output actionPageSetup_actionPerformed
	 */
	public void actionPageSetup_actionPerformed(ActionEvent e) throws Exception {
		super.actionPageSetup_actionPerformed(e);
	}

	/**
	 * output actionExitCurrent_actionPerformed
	 */
	public void actionExitCurrent_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionExitCurrent_actionPerformed(e);
	}

	/**
	 * output actionHelp_actionPerformed
	 */
	public void actionHelp_actionPerformed(ActionEvent e) throws Exception {
		super.actionHelp_actionPerformed(e);
	}

	/**
	 * output actionAbout_actionPerformed
	 */
	public void actionAbout_actionPerformed(ActionEvent e) throws Exception {
		super.actionAbout_actionPerformed(e);
	}

	/**
	 * output actionOnLoad_actionPerformed
	 */
	public void actionOnLoad_actionPerformed(ActionEvent e) throws Exception {
		super.actionOnLoad_actionPerformed(e);
	}

	/**
	 * output actionSendMessage_actionPerformed
	 */
	public void actionSendMessage_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionSendMessage_actionPerformed(e);
	}

	/**
	 * output actionCalculator_actionPerformed
	 */
	public void actionCalculator_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionCalculator_actionPerformed(e);
	}

	/**
	 * output actionExport_actionPerformed
	 */
	public void actionExport_actionPerformed(ActionEvent e) throws Exception {
		super.actionExport_actionPerformed(e);
	}

	/**
	 * output actionExportSelected_actionPerformed
	 */
	public void actionExportSelected_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionExportSelected_actionPerformed(e);
	}

	/**
	 * output actionRegProduct_actionPerformed
	 */
	public void actionRegProduct_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionRegProduct_actionPerformed(e);
	}

	/**
	 * output actionPersonalSite_actionPerformed
	 */
	public void actionPersonalSite_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionPersonalSite_actionPerformed(e);
	}

	/**
	 * output actionProcductVal_actionPerformed
	 */
	public void actionProcductVal_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionProcductVal_actionPerformed(e);
	}

	/**
	 * output actionExportSave_actionPerformed
	 */
	public void actionExportSave_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionExportSave_actionPerformed(e);
	}

	/**
	 * output actionExportSelectedSave_actionPerformed
	 */
	public void actionExportSelectedSave_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionExportSelectedSave_actionPerformed(e);
	}

	/**
	 * output actionKnowStore_actionPerformed
	 */
	public void actionKnowStore_actionPerformed(ActionEvent e) throws Exception {
		super.actionKnowStore_actionPerformed(e);
	}

	/**
	 * output actionAnswer_actionPerformed
	 */
	public void actionAnswer_actionPerformed(ActionEvent e) throws Exception {
		super.actionAnswer_actionPerformed(e);
	}

	/**
	 * output actionRemoteAssist_actionPerformed
	 */
	public void actionRemoteAssist_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionRemoteAssist_actionPerformed(e);
	}

	/**
	 * output actionPopupCopy_actionPerformed
	 */
	public void actionPopupCopy_actionPerformed(ActionEvent e) throws Exception {
		super.actionPopupCopy_actionPerformed(e);
	}

	/**
	 * output actionHTMLForMail_actionPerformed
	 */
	public void actionHTMLForMail_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionHTMLForMail_actionPerformed(e);
	}

	/**
	 * output actionExcelForMail_actionPerformed
	 */
	public void actionExcelForMail_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionExcelForMail_actionPerformed(e);
	}

	/**
	 * output actionHTMLForRpt_actionPerformed
	 */
	public void actionHTMLForRpt_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionHTMLForRpt_actionPerformed(e);
	}

	/**
	 * output actionExcelForRpt_actionPerformed
	 */
	public void actionExcelForRpt_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionExcelForRpt_actionPerformed(e);
	}

	/**
	 * output actionLinkForRpt_actionPerformed
	 */
	public void actionLinkForRpt_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionLinkForRpt_actionPerformed(e);
	}

	/**
	 * output actionPopupPaste_actionPerformed
	 */
	public void actionPopupPaste_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionPopupPaste_actionPerformed(e);
	}

	/**
	 * output actionAddNew_actionPerformed
	 */
	public void actionAddNew_actionPerformed(ActionEvent e) throws Exception {
		super.actionAddNew_actionPerformed(e);
	}

	/**
	 * output actionView_actionPerformed
	 */
	public void actionView_actionPerformed(ActionEvent e) throws Exception {
		super.actionView_actionPerformed(e);
	}

	/**
	 * output actionEdit_actionPerformed
	 */
	public void actionEdit_actionPerformed(ActionEvent e) throws Exception {
		super.actionEdit_actionPerformed(e);
	}

	/**
	 * output actionRemove_actionPerformed
	 */
	public void actionRemove_actionPerformed(ActionEvent e) throws Exception {
		super.actionRemove_actionPerformed(e);
	}

	/**
	 * output actionRefresh_actionPerformed
	 */
	public void actionRefresh_actionPerformed(ActionEvent e) throws Exception {
		super.actionRefresh_actionPerformed(e);
	}

	/**
	 * output actionPrint_actionPerformed
	 */
	public void actionPrint_actionPerformed(ActionEvent e) throws Exception {
		super.actionPrint_actionPerformed(e);
	}

	/**
	 * output actionPrintPreview_actionPerformed
	 */
	public void actionPrintPreview_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionPrintPreview_actionPerformed(e);
	}

	/**
	 * output actionLocate_actionPerformed
	 */
	public void actionLocate_actionPerformed(ActionEvent e) throws Exception {
		super.actionLocate_actionPerformed(e);
	}

	/**
	 * output actionQuery_actionPerformed
	 */
	public void actionQuery_actionPerformed(ActionEvent e) throws Exception {
		super.actionQuery_actionPerformed(e);
	}

	/**
	 * output actionImportData_actionPerformed
	 */
	public void actionImportData_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionImportData_actionPerformed(e);
	}

	/**
	 * output actionAttachment_actionPerformed
	 */
	public void actionAttachment_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionAttachment_actionPerformed(e);
	}

	/**
	 * output actionExportData_actionPerformed
	 */
	public void actionExportData_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionExportData_actionPerformed(e);
	}

	/**
	 * output actionToExcel_actionPerformed
	 */
	public void actionToExcel_actionPerformed(ActionEvent e) throws Exception {
		super.actionToExcel_actionPerformed(e);
	}

	/**
	 * output actionStartWorkFlow_actionPerformed
	 */
	public void actionStartWorkFlow_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionStartWorkFlow_actionPerformed(e);
	}

	/**
	 * output actionPublishReport_actionPerformed
	 */
	public void actionPublishReport_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionPublishReport_actionPerformed(e);
	}

	/**
	 * output actionCancel_actionPerformed
	 */
	public void actionCancel_actionPerformed(ActionEvent e) throws Exception {
		super.actionCancel_actionPerformed(e);
	}

	/**
	 * output actionCancelCancel_actionPerformed
	 */
	public void actionCancelCancel_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionCancelCancel_actionPerformed(e);
	}

	/**
	 * output actionQueryScheme_actionPerformed
	 */
	public void actionQueryScheme_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionQueryScheme_actionPerformed(e);
	}

	/**
	 * output actionWorkInsert_actionPerformed
	 */
	public void actionWorkInsert_actionPerformed(ActionEvent e)
			throws Exception {
		IRow row = this.tblAttendance.addRow();
		row.getCell("colRecordPerson").setValue(
				SysContext.getSysContext().getCurrentUserInfo().getPerson());
	}

	/**
	 * output actionWorkDelete_actionPerformed
	 */
	public void actionWorkDelete_actionPerformed(ActionEvent e)
			throws Exception {
		int[] idxs = KDTableUtil.getSelectedRows(this.tblAttendance);

		for (int i = 0; i < idxs.length; i++) {
			this.tblAttendance.removeRow(idxs[i]);
		}
	}

	/**
	 * output actionCata_actionPerformed
	 */
	public void actionCata_actionPerformed(ActionEvent e) throws Exception {
		super.actionCata_actionPerformed(e);
	}

	/**
	 * output actionCopyData_actionPerformed
	 */
	public void actionCopyData_actionPerformed(ActionEvent e) throws Exception {
		super.actionCopyData_actionPerformed(e);
	}

	/**
	 * output actionInput_actionPerformed
	 */
	public void actionInput_actionPerformed(ActionEvent e) throws Exception {
		super.actionInput_actionPerformed(e);
	}

	/**
	 * output actionOutput_actionPerformed
	 */
	public void actionOutput_actionPerformed(ActionEvent e) throws Exception {
		super.actionOutput_actionPerformed(e);
	}

	/**
	 * output actionSave_actionPerformed
	 */
	public void actionSave_actionPerformed(ActionEvent e) throws Exception {
		super.actionSave_actionPerformed(e);
	}

	/**
	 * output actionSelectCopy_actionPerformed
	 */
	public void actionSelectCopy_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionSelectCopy_actionPerformed(e);
	}

	@Override
	protected ICoreBase getBizInterface() throws Exception {
		return null;
	}

	@Override
	protected String getEditUIName() {
		return null;
	}

	@Override
	protected void btnQuickQuery_actionPerformed(ActionEvent e)
			throws Exception {

		this.orgUnit = ((AdminOrgUnitInfo) this.promptOrgUnit.getValue());
		FilterInfo outOrgRangeFilter = getOutOrgRangePersonFilter(this.orgUnit,
				this.isInclude, true);
		this.mainQuery.getFilter().mergeFilter(outOrgRangeFilter, "and");

		FilterInfo filter = new FilterInfo();
		if (!StringUtil.isEmpty(this.txtNameNum.getText())) {
			filter.getFilterItems().add(
					new FilterItemInfo("number", this.txtNameNum.getText(),
							CompareType.EQUALS));
			filter.getFilterItems().add(
					new FilterItemInfo("name", this.txtNameNum.getText(),
							CompareType.EQUALS));
			filter.setMaskString("#0 or #1");
			this.mainQuery.getFilter().mergeFilter(filter, "and");
		}

		this.tblMain.removeRows();
		this.mainQuery.getFilter().getFilterItems().clear();

		if (this.tblMain.getRowCount() > 0) {
			KDTableUtils.selectFirstRow(this.tblMain);
		}

	}

	@Override
	public void onLoad() throws Exception {
		super.onLoad();
		initSelector();
	}

	private Vector getTimeAttendanceList() throws BOSException {

		Vector list = new Vector();

		EntityViewInfo view = new EntityViewInfo();
		SelectorItemCollection selectors = new SelectorItemCollection();
		selectors.add("*");
		view.setSelector(selectors);

		FilterInfo fi = new FilterInfo();
		fi.getFilterItems().add(
				new FilterItemInfo("attendCatalog.id",
						Constants.ATTENDCATELOG_ATTENDTYPE_ID,
						CompareType.EQUALS));
		fi.getFilterItems().add(
				new FilterItemInfo("enable", true, CompareType.EQUALS));
		fi.setMaskString("#0 and #1");
		view.setFilter(fi);

		TimeAttendanceCollection collection = TimeAttendanceFactory
				.getRemoteInstance().getTimeAttendanceCollection(view);
		// list.add("");
		for (int i = 0; i < collection.size(); i++) {
			list.add(collection.get(i));
		}
		return list;
	}

	private void initSelector() {
		TimeAttendanceUtils.initF7Selector(this, this.promptOrgUnit);
		EmployeeClientUtils
				.initTableBtn(this.btnWorkInsert, this.btnWorkDelete);
	}

	private void initTblRecord() {

		KDTimePicker sumDate_DatePicker = new KDTimePicker();
		sumDate_DatePicker.setName("sumDate_DatePicker");
		sumDate_DatePicker.setVisible(true);
		sumDate_DatePicker.setEditable(true);

		KDTDefaultCellEditor sumDate_DatePicker_CellEditor = new KDTDefaultCellEditor(
				sumDate_DatePicker);
		this.tblAttendance.getColumn("colSumDate").setEditor(
				sumDate_DatePicker_CellEditor);

	}

	private FilterInfo getOutOrgRangePersonFilter(AdminOrgUnitInfo orgUnit,
			boolean isInclude, boolean isOutputQuery) {
		FilterInfo fi = new FilterInfo();

		String userID = SysContext.getSysContext().getCurrentUserInfo().getId()
				.toString();
		String hrOrgUnitID = SysContext.getSysContext().getCurrentHRUnit()
				.getId().toString();

		StringBuffer sql = new StringBuffer();
		sql
				.append(" SELECT P.FID FROM T_BD_PERSON P LEFT JOIN T_ORG_ADMIN ORG ON P.CFDEPTID=ORG.FID");
		sql.append(" WHERE ");
		sql.append(" P.CFDEPTID IN ");
		sql.append(" ( ");

		sql
				.append(" SELECT FFROMUNITID FROM T_ORG_UNITRELATION WHERE FTOUNITID = ");
		sql.append("'").append(hrOrgUnitID).append("'");
		sql
				.append(" AND FTYPERELATIONID = '00000000-0000-0000-0000-0000000000100FE9F8B5'");
		sql.append(" AND FFromUnitID IN ");
		sql.append("(");
		sql.append(" SELECT FOrgID FROM T_PM_OrgRange ");
		sql.append(" WHERE FUserID = ");
		sql.append("'").append(userID).append("'");
		sql.append(" and FType = 20 ");
		sql.append(")");
		sql.append(")");

		if (orgUnit != null) {
			if (isInclude) {
				sql.append(" and ");
				sql.append(" ( ");
				sql.append(" org.FLongNumber = '").append(
						orgUnit.getLongNumber()).append("'");
				sql.append(" or org.FLongNumber like '").append(
						orgUnit.getLongNumber()).append("%'");
				sql.append(" ) ");
			} else {
				sql.append(" and org.FLongNumber = '").append(
						orgUnit.getLongNumber()).append("'");
			}
		}

		ISQLExecutor exec = SQLExecutorFactory
				.getRemoteInstance(sql.toString());
		IRowSet rs = null;
		List listIds = new ArrayList();
		try {
			rs = exec.executeSQL();
		} catch (BOSException e) {
			handleException(e);
		}
		try {
			while (rs.next()) {
				if (rs.getString(1) == null)
					continue;
				listIds.add(rs.getString(1));
			}
		} catch (SQLException e) {
			handleException(e);
		}
		if (listIds.size() > 0) {
			fi.getFilterItems().add(
					new FilterItemInfo("id", new HashSet(listIds),
							CompareType.INNER));
		} else {
			listIds.add("");
			fi.getFilterItems().add(
					new FilterItemInfo("id", new HashSet(listIds),
							CompareType.INNER));
		}

		return fi;
	}

	@Override
	protected void tblMain_tableSelectChanged(KDTSelectEvent e)
			throws Exception {
		infoCollection.clear();
		IRow row = this.tblMain.getRow(e.getSelectBlock().getTop());
		if (row == null) {
			return;
		}

		infoCollection = this.getAttendanceDataLogCollection();

		fillTblAttendanceRecord(infoCollection);
	}

	private AttendanceDataLogCollection getAttendanceDataLogCollection()
			throws Exception {
		AttendanceDataLogCollection collection = null;

		IRow row = KDTableUtil.getSelectedRow(this.tblMain);

		String person = row.getCell("id").getValue().toString();

		EntityViewInfo evi = new EntityViewInfo();
		FilterInfo fi = new FilterInfo();

		fi.getFilterItems().add(new FilterItemInfo("person.id", person));

		if (this.datePickerStart.getValue() != null) {
			fi.getFilterItems().add(
					new FilterItemInfo("sumDate", this.datePickerStart
							.getValue(), CompareType.GREATER_EQUALS));
		}

		if (this.datePickerEnd.getValue() != null) {
			fi.getFilterItems().add(
					new FilterItemInfo("sumDate",
							this.datePickerEnd.getValue(),
							CompareType.LESS_EQUALS));
		}
		evi.getSelector().add("*");
		evi.getSelector().add("person.*");
		evi.getSelector().add("recordPerson.*");
		evi.getSelector().add("attendance.*");
		evi.setFilter(fi);
		collection = AttendanceDataLogFactory.getRemoteInstance()
				.getAttendanceDataLogCollection(evi);

		return collection;
	}

	private void fillTblAttendanceRecord(AttendanceDataLogCollection collection) {
		this.tblAttendance.removeRows();

		int size = collection.size();
		for (int i = 0; i < size; i++) {
			this.addRow(collection.get(i));
		}
	}

	private IRow addRow(AttendanceDataLogInfo info) {
		IRow curRow = this.tblAttendance.addRow();

		curRow.getCell("id").setValue(info.getId().toString());
		curRow.getCell("colAttendance").setValue(info.getAttendance());
		curRow.getCell("colTotalValue").setValue(info.getTotalValue());
		curRow.getCell("colTotalUnit").setValue(
				info.getAttendance().getUnitType());
		curRow.getCell("colSumDate").setValue(info.getSumDate());
		curRow.getCell("colRecordPerson").setValue(
				info.getRecordPerson().getName());
		curRow.getCell("colMemo").setValue(info.getDescription());

		return curRow;
	}

	private AttendanceDataLogInfo getAttendanceDataLogInfo(IRow row) {
		AttendanceDataLogInfo info = new AttendanceDataLogInfo();

		PersonInfo p = null;
		IRow personRow = KDTableUtil.getSelectedRow(this.tblMain);

		if (row.getCell("id").getValue() != null) {
			info.setId(BOSUuid.read(row.getCell("id").getValue().toString()));
		}

		if (personRow.getCell("id").getValue() != null) {
			p = new PersonInfo();
			p
					.setId(BOSUuid.read(personRow.getCell("id").getValue()
							.toString()));
			info.setPerson(p);
		}

		if (row.getCell("colAttendance").getValue() != null) {
			info.setAttendance((TimeAttendanceInfo) row
					.getCell("colAttendance").getValue());
		}

		// Time time = new Time();
		if (row.getCell("colTotalValue").getValue() != null) {
			info.setTotalValue(Float.valueOf(row.getCell("colTotalValue")
					.getValue().toString()));
		}

		if (row.getCell("colSumDate").getValue() != null) {
			info.setSumDate((Date) row.getCell("colSumDate").getValue());
		}

		if (row.getCell("colRecordPerson").getValue() != null) {
			info.setRecordPerson(SysContext.getSysContext()
					.getCurrentUserInfo().getPerson());
		}

		if (row.getCell("colMemo").getValue() != null) {
			info
					.setDescription(row.getCell("colSumDate").getValue()
							.toString());
		}

		info.setCU(SysContext.getSysContext().getCurrentCtrlUnit());

		if (info.getId() == null) {
			info.setCreator(SysContext.getSysContext().getCurrentUserInfo());
			info.setCreateTime(new Timestamp(new Date().getTime()));
		} else {
			info.setLastUpdateUser(SysContext.getSysContext()
					.getCurrentUserInfo());
			info.setLastUpdateTime(new Timestamp(new Date().getTime()));
		}

		return info;
	}

	@Override
	protected void tblAttendance_editStopped(KDTEditEvent e) throws Exception {
		int colIndex = e.getColIndex();
		if (colIndex == 1) {
			if (this.tblAttendance.getRow(e.getRowIndex()).getCell(colIndex)
					.getValue() != null) {
				TimeAttendanceInfo info = (TimeAttendanceInfo) this.tblAttendance
						.getRow(e.getRowIndex()).getCell(colIndex).getValue();
				this.tblAttendance.getRow(e.getRowIndex()).getCell(
						"colTotalUnit").setValue(info.getUnitType());
			}
		}
	}

	@Override
	protected void btnSave_actionPerformed(ActionEvent e) throws Exception {

		if (!KDTableUtilEx.checkTableRequired(this.tblAttendance, 0)) {
			SysUtil.abort();
		}
		int cnt = this.tblAttendance.getRowCount();
		IRow row = null;
		AttendanceDataLogInfo info = null;

		Set addSet = new HashSet();
		Set updateSet = new HashSet();
		Set deleteSet = new HashSet();
		Map configMap = new HashMap();

		for (int i = 0; i < cnt; i++) {
			row = this.tblAttendance.getRow(i);
			info = this.getAttendanceDataLogInfo(row);
			if (info.getId() != null) {
				updateSet.add(info);
				configMap.put(info.getId(), info);
			} else {
				addSet.add(info);
			}
		}

		for (int i = 0; i < this.infoCollection.size(); i++) {
			info = this.infoCollection.get(i);
			if (configMap.get(info.getId()) == null) {
				deleteSet.add(info);
			}
		}

		info = new AttendanceDataLogInfo();
		info.put("addSet", addSet);
		info.put("updateSet", updateSet);
		info.put("deleteSet", deleteSet);

		AttendanceDataLogFactory.getRemoteInstance()
				.saveAttendanceDataLog(info);

		infoCollection.clear();

		infoCollection = this.getAttendanceDataLogCollection();

		fillTblAttendanceRecord(infoCollection);
	}
}